package com.servlet.action;

import java.util.HashMap;
import java.util.Map;

/*
 * 动作工厂,使用反射构造动作类
 */
public class ActionFactory {
	protected  Map actmap = loadActions();
	public ActionFactory(){
		
	}
	
	//注册动作与相应浏览器请求的一一映射
	@SuppressWarnings("unchecked")
	protected Map loadActions(){
		Map map= new HashMap<String,Class>();
		map.put("LoginAction", LoginAction.class);
		map.put("SessionOvertimeAction", SessionOvertimeAction.class);
		map.put("SendMsgAction", SendMsgAction.class);
		map.put("UpdateMsgAction", UpdateMsgAction.class);
		map.put("UpdateOnlineUser", UpdateOnlineUser.class);
		map.put("RegistAction", RegistAction.class);	
		map.put("LeaveAction", LeaveAction.class);	
		return map;
	}
	
	//使用反射构造动作类
	public Action create(String actionName) {
		Class klass = (Class) actmap.get(actionName);
        if (klass == null)
             throw new RuntimeException(getClass() + " was unable to find "
              +"an action named " + actionName + "'.");
         
        Action actionInstance = null;
        try {
             actionInstance = (Action) klass.newInstance();
        } catch (Exception e) {
             e.printStackTrace();
        }
        return actionInstance; 
	}
}
